﻿// Copyright © 2010 Silvestru Iapascurta.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see license.txt for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Karoro
{
    public partial class FeedPreferencesDialog : Form
    {
        bool valuesChanged;

        public FeedPreferencesDialog()
        {
            InitializeComponent();
        }

        public DataTable Data
        {
            get;
            set;
        }

        private void FeedPreferencesDialog_Load(object sender, EventArgs e)
        {
            title_TB.Text = Data.Rows[0]["title"].ToString();
            web_TB.Text = Data.Rows[0]["web_url"].ToString();
            feed_TB.Text = Data.Rows[0]["xml_url"].ToString();
            update_interval_nUD.Value = (int)(long)Data.Rows[0]["update_interval"];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (valuesChanged)
            {
                Database.Execute("UPDATE feeds SET title = @title, web_url = @web_url, xml_url = @xml_url, update_interval = @update_interval WHERE feed_id = @feed_id",
                    new SQLiteParameter[] {
                        new SQLiteParameter("@title", title_TB.Text),
                        new SQLiteParameter("@web_url", web_TB.Text),
                        new SQLiteParameter("@xml_url", feed_TB.Text),
                        new SQLiteParameter("@update_interval", update_interval_nUD.Value),
                        new SQLiteParameter("@feed_id", Data.Rows[0]["feed_id"]) }
                    );
            }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void update_interval_nUD_ValueChanged(object sender, EventArgs e)
        {
            valuesChanged = true;
        }

        private void title_TB_TextChanged(object sender, EventArgs e)
        {
            valuesChanged = true;
        }

        private void feed_TB_TextChanged(object sender, EventArgs e)
        {
            valuesChanged = true;
        }

        private void web_TB_TextChanged(object sender, EventArgs e)
        {
            valuesChanged = true;
        }
    }
}
